<template>
  <div>
    <div class="permissionManagerTool">
      <el-input size="small" placeholder="请输入角色英文名" v-model="role.name">
        <template slot="prepend">ROLE_</template>
      </el-input>
      <el-input size="small" placeholder="请输入角色中文名" v-model="role.remark" @keydown.enter.native="doAddRole"></el-input>
      <el-button size="small" type="primary" icon="el-icon-plus" @click="doAddRole">添加角色</el-button>
    </div>
    <div class="permissionManagerMain">
      <el-collapse accordion @change="change" v-model="activeName">
        <el-collapse-item :title="r.remark" :name="r.id" v-for="(r,index) in roles" :key="index">
          <el-card class="box-card">
            <div slot="header" class="clearfix">
              <span>可访问资源</span>
              <el-button style="float: right; padding: 3px 0;color: #ff0000" type="text" icon="el-icon-delete" @click="doDeleteRole(r)"></el-button>
            </div>
            <div>
              <el-tree :data="allMenus" :props="defaultProps" @node-click="handleNodeClick" show-checkbox :default-checked-keys="selectMenus" node-key="id" ref="tree" :key="index"></el-tree>
              <div style="display: flex;justify-content: flex-end">
                <el-button size="small" @click="cancelUpdate">取消修改</el-button>
                <el-button type="primary" size="small" @click="doUpdate(r.id,index)">确认修改</el-button>
              </div>
            </div>
          </el-card>
        </el-collapse-item>
      </el-collapse>
    </div>
  </div>
</template>

<script>
export default {
  name: "PermissionManage",
  data() {
    return {
      role: {
        name: '',
        remark: ''
      },
      roles: [],
      allMenus: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      selectMenus: [],
      activeName: -1
    }
  },
  methods: {
    doDeleteRole(role) {
      this.$confirm('此操作将永久删除[' + role.name + ']角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteRequest('/system/basic/permission/role/' + role.id).then(res => {
          if (res) {
            this.initRoles()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    doAddRole() {
      if (this.role.name && this.role.remark) {
        this.postRequest('/system/basic/permission/role',this.role).then(res => {
          if (res) {
            this.initRoles()
            this.role.name = ''
            this.role.remark = ''
          }
        })
      }else {
        this.$message.error("字段不能为空！")
      }
    },
    initRoles() {
      this.getRequest('/system/basic/permission/').then(res => {
        this.roles = res
      })
    },
    handleNodeClick(data) {
      // console.log(data);
    },
    change(rid) {
      if (rid) {
        this.allMenus = []
        this.selectMenus = []
        this.initAllMenus()
        //根据rid获取菜单id
        this.initSelectMenus(rid)
      }
    },
    initAllMenus() {
      this.getRequest('/system/basic/permission/menus').then(res => {
        if (res) {
          this.allMenus = res
        }
      })
    },
    initSelectMenus(rid) {
      this.getRequest('/system/basic/permission/mid/' + rid).then(res => {
        if (res) {
          this.selectMenus = res
        }
      })
    },
    doUpdate(rid,index) {
      let selectedkeys = this.$refs.tree[index].getCheckedKeys(true)
      console.log(selectedkeys)
      let url = '/system/basic/permission/?rid=' + rid
      selectedkeys.forEach(key => {
        url += '&mids=' + key
      })
      this.putRequest(url).then(res => {
        if (res) {
          this.activeName = -1
        }
      })
    },
    cancelUpdate() {
      this.activeName = -1
    }
  },
  mounted() {
    this.initRoles()
  }
}
</script>

<style>
.permissionManagerTool {
  display: flex;
  justify-content: flex-start;
}
.permissionManagerTool .el-input {
  width: 300px;
  margin-right: 6px;
}
.permissionManagerMain {
  margin-top: 10px;
  width: 700px;
}
</style>